﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxTreeList;
using DevExpress.XtraPrinting;
using OT.Module.Model;
using OT.Module.BLL;
using OT.BaseLibrary;
using OT.Common;
using OT.Dev;
using OT.Module.Tree;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using DevExpress.Web.Data;
using System.ComponentModel;

public partial class Common_MasterDetail : DevPage
{
    /***
     *  Page
     */

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && !IsCallback)
        {
            // 其它控件初始化
            InitControlValue();
            // 风格设置
            InitGridStyle(InfoGrid, this.GetType().FullName);
        }
        // 调整分页风格
        GridPagerStyleSetting(InfoGrid, commonPageCtrl.PagerType);
    }

    #endregion

    #region InitControlValue

    protected void InitControlValue()
    {

        // 下面加入页面自定义控件初始化代码
    }

    #endregion

    /***
     *  Panel & Grid
     */

    #region ajaxPanel_Callback

    protected void ajaxPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        // 切换树节点时，要将列表之前选定的行取消
        InfoGrid.Selection.UnselectAll();
        // 数据源使用后期参数绑定，所以要调用DataBind
        InfoGrid.DataBind();
    }

    #endregion

    #region InfoGrid_PreRender

    protected void InfoGrid_PreRender(object sender, EventArgs e)
    {
        // 对列表的每一列进行自动的设置，包括
        // 1、根据字段长度自动调整列宽
        // 2、设置编辑页面的文本不换行
        AutoSettingGridColumnStyle(InfoGrid);
    }

    #endregion

    /***
     *  DataSource
     */

    #region ds_ObjectCreating

    protected void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new Order_BLL(XpoSession);
    }
    #endregion

    #region ds_DetailObjectCreating

    protected void ds_DetailObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new OrderDetail_BLL(XpoSession);
    }
    #endregion

    /***
     *  Master Grid data event
     */

    #region InfoGrid_CustomCallback

    protected void InfoGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        // 统一处理新增，批量删除，改变分页风格，打开/关闭过滤行事件
        if (HandleDefaultEvent(e.Parameters, InfoGrid, new Order_BLL(XpoSession)))
            return;

        // 不能统一处理的事件，由页面自行处理
    }
    #endregion

    #region InfoGrid_InitNewRow

    protected void InfoGrid_InitNewRow(object sender, ASPxDataInitNewRowEventArgs e)
    {
        e.NewValues.Add("OrderTime", DateTime.Now);
    }

    #endregion

    #region InfoGrid_RowInserting

    protected void InfoGrid_RowInserting(object sender, ASPxDataInsertingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        Order entity = new Order(XpoSession);

        new EntityMeta<Order>(XpoSession).SetEntityProperty(entity, e.NewValues);

        new Order_BLL(XpoSession).Insert(entity);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion

    #region InfoGrid_RowUpdating

    protected void InfoGrid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        EntityMeta<Order> meta = new EntityMeta<Order>(XpoSession);

        Order entity = XpoSession.GetObjectByKey<Order>(e.Keys[meta.KeyProperty].ToString());
        Order old = XpoSession.GetObjectByKey<Order>(e.Keys[meta.KeyProperty].ToString());

        meta.SetEntityProperty(entity, e.NewValues);

        new Order_BLL(XpoSession).Update(entity, old);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion

    #region InfoGrid_RowDeleting
    protected void InfoGrid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        string id = e.Keys[0].ToString();

        string error = "";

        // 未通过删除校验则取消随后发送到数据逻辑层的操作
        if (!new Order_BLL(XpoSession)._CheckDelete(id, ref error))
        {
            throw new ApplicationException(error);
        }

        new Order_BLL(XpoSession)._DeleteByKey(id);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion

    #region InfoGrid_HtmlRowCreated

    protected void InfoGrid_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Detail)
        {
            // 子表是动态建立的，因此每次都要设置风格
            ASPxGridView detailGrid = (ASPxGridView)InfoGrid.FindDetailRowTemplateControl(e.VisibleIndex, "DetailGrid");

            detailGrid.ClientInstanceName = string.Format("DetailGrid_{0}", e.VisibleIndex);
            UseControl_PageCommon pageCommon = (UseControl_PageCommon)InfoGrid.FindDetailRowTemplateControl(e.VisibleIndex, "commonDetailPageCtrl");
            pageCommon.GridClientName = detailGrid.ClientInstanceName;
            pageCommon.ReInit();

            InitGridStyle(detailGrid, this.GetType().FullName + ".DetailGrid");
            AutoSettingGridColumnStyle(detailGrid);
        }
    }
    #endregion
    
    /***
     *  Detail Grid data event
     */

    #region detailGrid_DataSelect

    protected void detailGrid_DataSelect(object sender, EventArgs e)
    {
        ASPxGridView detailGrid = (sender as ASPxGridView);

        // 将主表行的ID值保存在Session,提供给数据源使用
        Session["OrderID"] = detailGrid.GetMasterRowKeyValue();
    }

    #endregion

    #region detailGrid_CustomCallback

    protected void detailGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        // 统一处理新增，批量删除，改变分页风格，打开/关闭过滤行事件
        if (HandleDefaultEvent(e.Parameters, sender as ASPxGridView, new OrderDetail_BLL(XpoSession)))
            return;

        // 不能统一处理的事件，由页面自行处理
    }

    #endregion

    #region detailGrid_InitNewRow

    protected void detailGrid_InitNewRow(object sender, ASPxDataInitNewRowEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
    }

    #endregion

    #region detailGrid_RowInserting

    protected void detailGrid_RowInserting(object sender, ASPxDataInsertingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        OrderDetail entity = new OrderDetail(XpoSession);

        new EntityMeta<OrderDetail>(XpoSession).SetEntityProperty(entity, e.NewValues);

        Order order = XpoSession.GetObjectByKey<Order>(grid.GetMasterRowKeyValue());
        entity.BelongOrder = order;

        new OrderDetail_BLL(XpoSession).Insert(entity);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion

    #region detailGrid_RowUpdating

    protected void detailGrid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        EntityMeta<OrderDetail> meta = new EntityMeta<OrderDetail>(XpoSession);

        OrderDetail entity = XpoSession.GetObjectByKey<OrderDetail>(e.Keys[meta.KeyProperty].ToString());
        OrderDetail old = XpoSession.GetObjectByKey<OrderDetail>(e.Keys[meta.KeyProperty].ToString());

        meta.SetEntityProperty(entity, e.NewValues);

        new OrderDetail_BLL(XpoSession).Update(entity, old);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion

    #region detailGrid_RowDeleting
    protected void detailGrid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        string id = e.Keys[0].ToString();

        string error = "";

        // 未通过删除校验则取消随后发送到数据逻辑层的操作
        if (!new OrderDetail_BLL(XpoSession)._CheckDelete(id, ref error))
        {
            throw new ApplicationException(error);
        }

        new OrderDetail_BLL(XpoSession)._DeleteByKey(id);

        DataSourceRefreshCancel(grid, e);
    }
    #endregion


    /***
     *  Validate
     */

    #region InfoGrid_RowValidating

    protected void InfoGrid_RowValidating(object sender, ASPxDataValidationEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        Order entity = new Order(XpoSession);

        new EntityMeta<Order>(XpoSession).SetEntityProperty(entity, e.NewValues);

        ValidationMDL<Order>(entity, grid, e);
    }

    #endregion

    #region detailGrid_RowValidating

    protected void detailGrid_RowValidating(object sender, ASPxDataValidationEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;

        OrderDetail entity = new OrderDetail(XpoSession);

        new EntityMeta<OrderDetail>(XpoSession).SetEntityProperty(entity, e.NewValues);

        ValidationMDL<OrderDetail>(entity, grid, e);
    }

    #endregion

    /***
     *  Export
     */

    #region btnExport_Click

    protected void btnExport_Click(object sender, EventArgs e)
    {
        UseControl_PageCommon pc = sender as UseControl_PageCommon;

        // 将表格数据导出
        Export(pc.GridExpoter, pc.FileType);
    }

    #endregion

}
